package cate.game.util;

import easy.java.util.SnowflakeIdWorker;
import org.bson.types.ObjectId;

import java.util.concurrent.atomic.AtomicInteger;

public class GameKit {

    /** 生成一个uid */
    public static String uid() {
        return ObjectId.get().toHexString();
    }

    private static AtomicInteger workerCounter = new AtomicInteger(0);
    private static AtomicInteger dcCounter = new AtomicInteger(0);

    public static long buildLong() {
        int dc = dcCounter.incrementAndGet();
        int wc = workerCounter.get();
        if(dc > 31) {
            dcCounter.set(0);
            dc = 0;
            wc = workerCounter.incrementAndGet();
        }
        if(wc > 31) {
            workerCounter.set(0);
            wc = 0;
        }
        //System.out.println("wc=" + wc + " dc=" + dc);
        SnowflakeIdWorker worker = new SnowflakeIdWorker(wc, dc);
        return worker.nextId();
    }
}
